﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/**
 * 控制子弹的移动、碰撞
 **/
public class BulletController : MonoBehaviour
{
    Rigidbody2D rbody;

    public AudioClip hitClip;//命中音效
    // Start is called before the first frame update
    void Awake()
    {
        rbody = GetComponent<Rigidbody2D>();
        Destroy(this.gameObject, 2f);//2秒后销毁
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    /**
     * 子弹的移动
     **/
    public void Move(Vector2 moveDirection,float moveForce) {
        rbody.AddForce(moveDirection * moveForce);//给子弹一个朝向的力
    }
    /**
     * 碰撞检测
     **/
    void OnCollisionEnter2D(Collision2D other) {
        EnemyController ec = other.gameObject.GetComponent<EnemyController>();
        if (ec != null) {
            Debug.Log("碰到敌人了");
            ec.Fixed();//修复敌人
        }
        AudioManager.instance.AudioPlay(hitClip);//命中音效
        Destroy(this.gameObject);//销毁
    }
}
